#!/bin/bash

echo "Assembling... real.asm"
nasm kernel/real.asm -f bin -o bin/real.bin -l real.lst

echo "Assembling... start.asm"
nasm -f aout -o obj/start.asm.o kernel/start.asm

echo "Assembling... syscall.asm"
nasm -f aout -o obj/syscall.asm.o kernel/syscall.asm

echo "Compiling... sys.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/sys.o kernel/sys.c

echo "Compiling... syscall.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/syscall.o kernel/syscall.c

echo "Compiling... real.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/real.o kernel/real.c

echo "Compiling... boot.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/boot.o kernel/boot.c

echo "Compiling... mm.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/mm.o kernel/mm.c

echo "Compiling... mt.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/mt.o kernel/mt.c

echo "Compiling... paging.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/paging.o kernel/paging.c

echo "Compiling... gdt.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/gdt.o kernel/gdt.c

echo "Assembling... gdt.asm"
nasm -f aout -o obj/gdt.asm.o kernel/gdt.asm

echo "Compiling... idt.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/idt.o kernel/idt.c

echo "Assembling... idt.asm"
nasm -f aout -o obj/idt.asm.o kernel/idt.asm

echo "Compiling... isr.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/isr.o kernel/isr.c

echo "Assembling... isr.asm"
nasm -f aout -o obj/isr.asm.o kernel/isr.asm

echo "Compiling... irq.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/irq.o kernel/irq.c

echo "Assembling... irq.asm"
nasm -f aout -o obj/irq.asm.o kernel/irq.asm

echo "Compiling... timer.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/timer.o kernel/timer.c

echo "Compiling... kb.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/kb.o kernel/kb.c

echo "Compiling... floppy.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/floppy.o kernel/floppy.c

echo "Compiling... cli.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/cli.o kernel/cli.c

echo "Compiling... libs/memory.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/libs/memory.o kernel/libs/memory.c

echo "Compiling... libs/string.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/libs/string.o kernel/libs/string.c

echo "Compiling... libs/rect.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/libs/rect.o kernel/libs/rect.c

echo "Compiling... video/video.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/video/video.o kernel/video/video.c

echo "Compiling... video/vgafont.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/video/vgafont.o kernel/video/vgafont.c

echo "Compiling... video/vgatext.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/video/vgatext.o kernel/video/vgatext.c

echo "Compiling... video/vgagraphics.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/video/vgagraphics.o kernel/video/vgagraphics.c

echo "Compiling... video/vgabase.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/video/vgabase.o kernel/video/vgabase.c

echo "Compiling... video/vesa.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./kernel/h -c -o obj/video/vesa.o kernel/video/vesa.c

echo "Compiling... video/drawer.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/video/drawer.o kernel/video/drawer.c

echo "Compiling... ui/ui.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/ui.o kernel/ui/ui.c

echo "Compiling... ui/headerfooter.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/headerfooter.o kernel/ui/headerfooter.c

echo "Compiling... ui/tabs/programs.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/tabs/programs.o kernel/ui/tabs/programs.c

echo "Compiling... ui/tabs/folders.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/tabs/folders.o kernel/ui/tabs/folders.c

echo "Compiling... ui/tabs/settings.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/tabs/settings.o kernel/ui/tabs/settings.c

echo "Compiling... ui/tabs/terminal.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/tabs/terminal.o kernel/ui/tabs/terminal.c

echo "Compiling... ui/tabs/power.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/tabs/power.o kernel/ui/tabs/power.c

echo "Compiling... ui/tabs/about.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/tabs/about.o kernel/ui/tabs/about.c

echo "Compiling... ui/programs/crashtest.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/programs/crashtest.o kernel/ui/programs/crashtest.c

echo "Compiling... ui/programs/debuginfo.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/programs/debuginfo.o kernel/ui/programs/debuginfo.c

echo "Compiling... ui/programs/vgatest.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/programs/vgatest.o kernel/ui/programs/vgatest.c

echo "Compiling... ui/programs/cpuidinfo.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/programs/cpuidinfo.o kernel/ui/programs/cpuidinfo.c

echo "Compiling... ui/programs/floppytest.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/ui/programs/floppytest.o kernel/ui/programs/floppytest.c

echo "Assembling... cpuid.asm"
nasm -f aout -o obj/cpuid.asm.o kernel/cpuid.asm

echo "Compiling... cpuid.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/cpuid.o kernel/cpuid.c

echo "Compiling... cmos.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/cmos.o kernel/cmos.c

echo "Compiling... rtc.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/rtc.o kernel/rtc.c

echo "Compiling... acpi.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/acpi.o kernel/acpi.c

echo "Compiling... power.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/power.o kernel/power.c

echo "Compiling... main.c"
gcc -m32 -Wall -O0 -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-stack-protector -I./kernel/h -c -o obj/main.o kernel/main.c

echo "Linking... kernel.bin"
ld -T link.ld -o bin/kernel.bin obj/start.asm.o obj/syscall.asm.o obj/sys.o obj/syscall.o obj/real.o obj/boot.o obj/mm.o obj/mt.o obj/paging.o obj/gdt.asm.o obj/gdt.o obj/idt.asm.o obj/idt.o obj/isr.asm.o obj/isr.o obj/irq.asm.o obj/irq.o obj/timer.o obj/kb.o obj/floppy.o obj/cli.o obj/libs/memory.o obj/libs/string.o obj/libs/rect.o obj/video/video.o obj/video/vgafont.o obj/video/vgatext.o obj/video/vgagraphics.o obj/video/vgabase.o obj/video/vesa.o obj/video/drawer.o obj/ui/ui.o obj/ui/headerfooter.o obj/ui/tabs/programs.o obj/ui/tabs/folders.o obj/ui/tabs/settings.o obj/ui/tabs/terminal.o obj/ui/tabs/power.o obj/ui/tabs/about.o obj/ui/programs/crashtest.o obj/ui/programs/debuginfo.o obj/ui/programs/vgatest.o obj/ui/programs/cpuidinfo.o obj/ui/programs/floppytest.o obj/cpuid.asm.o obj/cpuid.o obj/cmos.o obj/rtc.o obj/acpi.o obj/power.o obj/main.o > ld.txt

echo "Updating floppy image... floppy.img"
sudo /sbin/losetup /dev/loop0 img/floppy.img
sudo mkdir Mount
sudo mount /dev/loop0 Mount
sudo cp bin/kernel.bin Mount/kernel.bin
sudo cp img/menu.lst Mount/boot/grub/menu.lst
sudo umount /dev/loop0
sudo /sbin/losetup -d /dev/loop0
sudo rmdir Mount


echo "Done!"
